package com.rustic.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rustic.server.dao.CategoryDao;
import com.rustic.server.enums.CategoryType;
import com.rustic.server.po.Category;
import com.rustic.server.query.CategoryAdminListQuery;
import com.rustic.server.service.CategoryService;
import com.rustic.server.vo.list.CategoryAdminListVO;
import com.rustic.server.vo.list.CategoryWebListVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Category对应的服务类实现
 *
 * @author auto
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Override
    public List<Category> listByIdIn(List<Integer> idList) {
        if (CollUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return this.listByIds(idList);
    }

    @Override
    public Map<Integer, Category> mapByIdIn(List<Integer> idList) {
        List<Category> categoryList = this.listByIdIn(idList);
        return categoryList.stream().collect(Collectors.toMap(Category::getId, b -> b));
    }

    @Override
    public LambdaQueryWrapper<Category> toAdminListWrapper(CategoryAdminListQuery query) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(CharSequenceUtil.isNotEmpty(query.getName()), Category::getName, query.getName());
        queryWrapper.in(CollUtil.isNotEmpty(query.getType()), Category::getType, query.getType());
        queryWrapper.gt(query.getStartCreateTime() != null, Category::getCreateTime, query.getStartCreateTime());
        queryWrapper.lt(query.getEndCreateTime() != null, Category::getCreateTime, query.getEndCreateTime());
        queryWrapper.orderByDesc(Category::getSort);
        return queryWrapper;
    }

    @Override
    public List<CategoryAdminListVO> toAdminListVo(List<Category> categoryList) {
        List<CategoryAdminListVO> results = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryAdminListVO categoryAdminListVO = new CategoryAdminListVO(category);
            results.add(categoryAdminListVO);
        }
        return results;
    }

    @Override
    public List<CategoryWebListVO> toWebListVo(List<Category> categoryList) {
        List<CategoryWebListVO> results = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryWebListVO categoryWebListVO = new CategoryWebListVO(category);
            results.add(categoryWebListVO);
        }
        return results;
    }

    @Override
    public List<Category> listByType(CategoryType categoryType) {
        return this.list(new LambdaQueryWrapper<Category>()
                .eq(Category::getType, categoryType.getValue()));
    }

    @Override
    public List<Category> listByTypes(List<Integer> categoryType) {
        if (CollUtil.isEmpty(categoryType)) {
            return new ArrayList<>();
        }
        return this.list(new LambdaQueryWrapper<Category>()
                .in(Category::getType, categoryType));
    }

    @Override
    public Category getByIdAndType(int id, CategoryType categoryType) {
        return this.getOne(new LambdaQueryWrapper<Category>()
                .eq(Category::getId, id)
                .eq(Category::getType, categoryType.getValue()));
    }
}
